package com.beverage.lemonade.databse;

import java.util.Iterator;
import java.util.List;

import com.beverage.lemonade.process.Constants;
import com.beverage.lemonade.process.ContentJson;
import com.beverage.lemonade.process.DeleteJson;
import com.beverage.lemonade.process.MyJson;
import com.beverage.lemonade.process.UserInfoJson;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;

public class JsonDataConnector extends DataConnector {

	public JsonDataConnector(Context context) {
		super(context);
	}

	public void saveUserInfo(UserInfoJson userInfoJson) {
		SharedPreferences sp = PreferenceManager
				.getDefaultSharedPreferences(context);
		sp.edit().putString(Constants.ID, userInfoJson.getID()).commit();
		sp.edit().putString(Constants.USERNAME, userInfoJson.getUSERNAME())
				.commit();
		sp.edit().putString(Constants.PASSWORD, userInfoJson.getPASSWORD())
				.commit();
		sp.edit().putInt(Constants.ISGRANTED, userInfoJson.getISGRANTED())
				.commit();
		sp.edit()
				.putInt(Constants.CREATED_DATE, userInfoJson.getCREATED_DATE())
				.commit();
		sp.edit().putString(Constants.MARKER1, userInfoJson.getMARKER1())
				.commit();
		sp.edit().putString(Constants.MARKER2, userInfoJson.getMARKER2())
				.commit();
		sp.edit().putString(Constants.MARKER3, userInfoJson.getMARKER3())
				.commit();
		sp.edit().putString(Constants.MARKER4, userInfoJson.getMARKER4())
				.commit();
	}

	public void saveAllContent(List<ContentJson> contentList) {
		Iterator ite = contentList.iterator();
		while (ite.hasNext()) {
			ContentJson contentJson = (ContentJson) ite.next();
			saveContent(contentJson);
		}
	}

	public void saveRemainder(DeleteJson deleteJson) {
		Iterator ite = deleteJson.getDeleteList().iterator();
		while (ite.hasNext()) {
			int Content_ID = (Integer) ite.next();
			hlpr.insertRemainder(Content_ID, Constants.OPERATION_DELETE);
		}
	}

	public void deleteContent(DeleteJson deleteJson) {
		Iterator ite = deleteJson.getDeleteList().iterator();
		while (ite.hasNext()) {
			int Content_ID = (Integer) ite.next();
			hlpr.deleteContent(Content_ID);
		}
	}

	public void saveRemainder(ContentJson contentJson) {
		hlpr.insertRemainder(contentJson.getCONTENT_ID(),
				Constants.OPERATION_ADD);

	}

	public void saveRemainder(List<ContentJson> contentList) {

		Iterator ite = contentList.iterator();
		while (ite.hasNext()) {
			ContentJson contentJson = (ContentJson) ite.next();
			hlpr.insertRemainder(contentJson.getCONTENT_ID(),
					Constants.OPERATION_DELETE);
		}

	}

	public void saveAllContent(MyJson myJson) {
		saveAllContent(myJson.getContentMap());
	}

	public void saveAll(MyJson myJson) {
		saveUserInfo(myJson.getUserInfoJson());
		saveAllContent(myJson.getContentMap());
	}

	public void saveContent(ContentJson contentJson) {

		saveContent(contentJson.getCONTENT_ID(), contentJson.getUPDATE_DATE(),
				contentJson.getDATE(), contentJson.getCONTENT(),
				contentJson.getMEMO(), contentJson.getNUMBER());
	}
}
